// pages/assessment/assessment.js
var app = getApp();
var ea = require('../../utils/each.js');
var time = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buzhizuoye: app.data.url +'/zhongxue/teacher/tijiaozuoye',
    gettoken: app.data.url + '/zhongxue/teacher/gettoken',
    shuju:{},
    testpaper: [],
    xiaxiaurl:'/img/26.png',
    titles: [
      {
        url: "/img/26.png",
        name: "陈雪"
      },
      {
        url: "/img/26.png",
        name: "陈雪"
      }, {
        url: "/img/26.png",
        name: "陈雪"
      },
      {
        url: "/img/26.png",
        name: "陈雪"
      },
      {
        url: "/img/26.png",
        name: "陈雪"
      },
      {
        url: "/img/26.png",
        name: "陈雪"
      },
      {
        url: "/img/26.png",
        name: "陈雪"
      },
      {
        url: "/img/26.png",
        name: "陈雪"
      }
    ],
    wrongUrl: "/img/20.png",
    pictureTile: [
      // {
      //   rollUrl:"/img/d96aef7937cafff8f012003eaa1355e.png",
      //   wrongUrl:"/img/20.png"
      // },
      // {
      //   rollUrl: "/img/d96aef7937cafff8f012003eaa1355e.png",
      //   wrongUrl: "/img/20.png"
      // },
      // {
      //   rollUrl: "/img/d96aef7937cafff8f012003eaa1355e.png",
      //   wrongUrl: "/img/20.png"
      // },
      // {
      //   rollUrl: "/img/d96aef7937cafff8f012003eaa1355e.png",
      //   wrongUrl: "/img/20.png"
      // }, 
      // {
      //   rollUrl: "/img/d96aef7937cafff8f012003eaa1355e.png",
      //   wrongUrl: "/img/20.png"
      // }
    ],

    plusUrl:[
      "/img/f0604ce67f88cedb8d7c78b22acc60f.png"
    ]
  },

  /**
   * 上传图片
   */
  shangchuan:function(){
    var that = this;
    var pictureTile = that.data.pictureTile;
    var shuju = that.data.shuju;
    wx.chooseImage({
      count: 9,
      // sizeType: [],
      // sourceType: [],
      success: function(res) {
        // console.log(res);
        var tempFilePaths = res.tempFilePaths;
        if (pictureTile.length + tempFilePaths.length>9){
          wx.showToast({
            title: '最多只能上传9张',
            icon:'none'
          })
          return
        }else{
          ea.upfile(tempFilePaths, function (newname) {
            // console.log(newname);
            pictureTile = ea.addArray(pictureTile, newname);
            // console.log(pictureTile);
            that.setData({ pictureTile: pictureTile })
          }, that.data.gettoken)
          shuju.tupians = pictureTile;
          console.log(shuju);
        }
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },

  /**
   * 删除图片
   */
  deltupian:function(e){
      // console.log(e);
      var index = e.currentTarget.dataset.index;
      var testpaper = this.data.testpaper;
      var shuju = this.data.shuju;
      var newtestpaper = [];
      ea.each(testpaper, function (i, v) {
        console.log(i)
        console.log(index);
        if (i !== index) {
          newtestpaper.push(v);
        }
      })
      this.setData({ testpaper: newtestpaper })
    },


  /**
   * 删除学生
   */
  shanchu:function(e){
    console.log(e)
    var index = e.currentTarget.dataset.index;
    var titles = this.data.titles;
    var newtitles = [];
    ea.each(titles,function(i,v){
      if(i!==index){
        newtitles.push(v);
      }
    })
    this.setData({ titles: newtitles})
    // console.log(newtitles);
    // var shuju = this.data.shuju;
    // shuju['student_id'] = 
    // console.log(shuju);
  },

  //获取input框所有内容
  inputWacth: function (e) {
    // console.log(e);
    let item = e.currentTarget.dataset.model;
    // console.log(item);
    var shuju = this.data.shuju;
    var user = wx.getStorageSync('newuser');
    shuju[item] = e.detail.value;
    // console.log(shuju[item])
    shuju['teacher_id'] = user.id;
    this.setData({ shuju: shuju });
    this.setData({
      [item]: e.detail.value
    });
    console.log(shuju)
  },

  /**
   * 布置作业
   */
  buzhi: function () {
    var that = this;
    var shuju = that.data.shuju;
    // console.log(user)
    // var token = user.token;
    // var shenhe = user.shenhe;
    var yanz = ea.yanzhen(shuju, {
      dir: '请输入作业详情',
      student_id: '请至少选择一个学生'
    })
    if (yanz) {
      wx.showToast({
        title: yanz,
        icon: 'none'
      })
      return;
    }
    wx.request({
      url: that.data.buzhizuoye,
      data:shuju,
      method:'post',
      success:function(res){
        var data = res.data
        console.log(data)
        if(data.code == 200){
          wx.showToast({
            title: '作业布置成功',
            icon: 'success',
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            })
          }, 1000)
        }else{
          wx.showToast({
            title: '作业布置失败',
            icon: 'none',
          })
        }
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   * 
   */
  onLoad: function (options) {
    // console.log(options)
    var curriculums = options.curriculums;
    // this.setData({ curriculums: shuju['curriculums']})
    var titles = wx.getStorageSync('jintian');
    this.setData({ titles: titles})
    wx.setStorageSync('jintian', null);
    var shuju = this.data.shuju;
    var studentid = [];
    var curriculums = curriculums;
    // shuju['student_id'] = titles
    ea.each(titles,function(i,v){
      studentid.push(v.id);
    })
    shuju.student_id = studentid;
    shuju.curriculum = curriculums;
    console.log(shuju)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})